package com.kevin.demo.aspect;

import com.kevin.demo.annotation.AbleInsertLog;
import com.kevin.demo.entity.log.Log;
import com.kevin.demo.entity.log.TestLog;
import com.kevin.demo.mapper.log.LogMapper;
import com.kevin.demo.mapper.log.TestLogMapper;
import com.kevin.demo.utils.ContextUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: e20160504
 * @date: 2018/7/27
 * @desc 新增时需要记录的日志
 */

@Aspect
@Component
public class LogInsertAspect {

    @Autowired
    private TestLogMapper mapper;

    @Before("@annotation(ableInsertLog)")
    public void doBefore(JoinPoint joinPoint, AbleInsertLog ableInsertLog){
        //获取新增的对象
        Object args = joinPoint.getArgs()[0];
        //初始化日志
        TestLog log = new TestLog();
        log.setNewObject(args.toString());
        log.setModifyname(ableInsertLog.oprName());
        log.setModifyobject(ableInsertLog.handleName());
        log.setUsername(ContextUtil.getUsername());
        mapper.insertSelective(log);
    }
}

























